<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use App\Qingwuit\Models\InviteCode;
use App\Qingwuit\Models\Invites;
use App\Qingwuit\Models\Retails;
use Illuminate\Http\Request;

class GoodsController extends Controller
{
    public function goods()
    {
//        echo 111;die;
        return $this->handle($this->getService('Goods')->all());
    }

    public function show($id)
    {
//dd(1);
        $user_id = $this->getUserId('users');

        $goods_info = $this->getService('Goods')->getGoodsInfo($id);
//        print_r($goods_info);
//        $goods_info['data']['goods_content'];
        $binding = Retails::where('distribution_id', $goods_info['data']['store_id'])->where('shop_id', $user_id)->first();

        $sku = $this->getService('GoodsSku', true)->where('goods_id', $id)->get()->toArray();
        if (!empty($sku)){

            if (is_null($binding)) {

                $goods_info['data']['goods_price'] = "***";
                $skuList = $goods_info['data']['skuList']; // 获取skuList
                foreach ($skuList as $k => $v) {
                    $skuList[$k]['goods_price'] = "***"; // 修改skuList中的goods_price
                }
                $goods_info['data']['skuList'] = $skuList; // 将修改后的skuList重新赋值给$goods_info['data']
            }else{
                $biliId = InviteCode::where('code',$binding->code)->first();
                $bili = Invites::where('id',$biliId->pid)->first();
                $goods_info['data']['goods_price'] = round($goods_info['data']['goods_price'] + $goods_info['data']['goods_price'] * $bili->percentage/100,2);
                $skuList = $goods_info['data']['skuList']; // 获取skuList
                foreach ($skuList as $k => $v) {
                    $skuList[$k]['goods_price'] = round( $skuList[$k]['goods_price'] +  $skuList[$k]['goods_price'] * $bili->percentage/100,2);; // 修改skuList中的goods_price
                }
                $goods_info['data']['skuList'] = $skuList; // 将修改后的skuList重新赋值给$goods_info['data']
            }
        }else{
            if (is_null($binding)) {

                $goods_info['data']['goods_price'] = "***";
            }else{
                $biliId = InviteCode::where('code',$binding->code)->first();
                $bili = Invites::where('id',$biliId->pid)->first();
                $goods_info['data']['goods_price'] = round($goods_info['data']['goods_price'] + $goods_info['data']['goods_price'] * $bili->percentage/100,2);
            }
        }

        if ($goods_info['status']) {
            $goods_info['data']['store_info'] = $this->getService('Store')->getStoreInfoAndRate($goods_info['data']['store_id'])['data'];
            $goods_info['data']['rate'] = $goods_info['data']['store_info']['rate'];
            $goods_info['data']['comment_statistics'] = $this->getService('OrderComment')->commentStatistics($goods_info['data']['id'])['data'];
            $goods_info['data']['sale_list'] = $this->getService('Goods')->sortGoods($goods_info['data']['store_id'], 'store_id', 6, ['class_id'=>$goods_info['data']['class_id']])['data']; // 销售排名
            $goods_info['data']['coupons'] = $this->getService('Coupon')->getCouponByStoreId($goods_info['data']['store_id'])['data']; // 优惠券
            // $goods_info['data']['full_reductions'] = $full_reduction_service->getFullReductionByStoreId($goods_info['data']['store_id'])['data']; // 满减
            $seckill_info = $this->getService('Seckill')->seckillInfoByGoodsId($id);
            $goods_info['data']['seckills'] = $seckill_info['status']?$seckill_info['data']:false; // 秒杀
            $collective_info = $this->getService('Collective')->getCollectiveInfoByGoodsId($id);
            $goods_info['data']['collectives'] = $collective_info['status']?$collective_info['data']:false; // 团购
            $goods_info['data']['collective_list'] = $this->getService('Collective')->getCollectiveLogByGoodsId($id)['data']; // 正在团的
        }
        return $this->handle($goods_info);
    }

    public function goods_comments($id)
    {
        $id = intval($id);
        return $this->handle($this->getService('OrderComment')->commentList($id));
    }
}
